//
//  YuYueShangPinLeiMuViewController.m
//  FD
//
//  Created by xiaofang wang on 2018/11/23.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  更 ShangPinLeiMuViewController  一样

#import "YuYueShangPinLeiMuViewController.h"
//model
#import "SPLMModel.h"
//view
#import "NewShopCartBottomView.h"
#import "ShoppingCartView.h"
#import "ShangPinLeiMuCustomView.h" //封装tableView及其代理方法

//viewController
//#import "WaiSongViewController.h"
#import "YuYueDianCanCartViewController.h" //预约左右tableView页面
#import "YuYueSureOrderViewController.h" //往预约确认订单页面跳转

//工具类
#import "YingYeShiJianKeFuDianHuaHelper.h" //获取营业事件
#import "HomeShopCartHelper.h" //计算购物车总价


@interface YuYueShangPinLeiMuViewController ()<
ShopCartViewDelegate,
ShoppingCartViewDelegate>


/**封装tableView及其代理方法*/
@property (nonatomic, strong) ShangPinLeiMuCustomView *customView;

/**底部购物车view*/
@property (nonatomic, strong) NewShopCartBottomView *bottomView;

/**点击购物车按钮 弹出购物车里面的内容的view 默认隐藏 点击购物车按钮显示*/
@property (nonatomic, strong) ShoppingCartView *shopcartView;

/**10-25购物车视图删除还是加载 用于标记是否显示购物车页面*/
@property (assign, nonatomic) BOOL isShopping;

/**11-21用于标记是否可以去结算
 如果计算出来的购物车金额为 < 0.0 就允许点击
 */
@property (nonatomic) NSInteger sumPrice;

@end

@implementation YuYueShangPinLeiMuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupViewAnnLoadData]; //请求本业数据
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [HomeShopCartHelper shopcartHelperTotalPriceBlock:^(CGFloat totalPrice) {
        [self.bottomView updateshopcartBottomViewTotalPrice:totalPrice];
        
        self.sumPrice = totalPrice; //标记购物车金额是否为0
    }];
    RBFunctionLog;
}

- (void)setupViewAnnLoadData {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //国际化
    NSString *yysplmStr = NSLocalizedString(YuYueGoodsList, @"门店预约商品类目");
    self.navigationItem.title = yysplmStr;

    
    //TABLEView
    //tableViweframe ? 为什么cell和tv的顶部和底部都有间距？
    CGRect tempFram = self.view.bounds;
//    tempFram.origin.y = FD_NavH; //yo
    tempFram.size.height = SCREEN_HEIGHT - FD_TabbarSafeBottomMargin - 50;
    self.customView.frame = tempFram;
    //    self.tv.contentInset = UIEdgeInsetsMake(-10, 0, -10, 0);
    [self.view addSubview:self.customView];
    
    //底部购物车view
    CGFloat bottomView_y = self.customView.rb_bottom;
    self.bottomView.frame = CGRectMake(0, bottomView_y, SCREEN_WIDTH, 50);
    [self.view addSubview:self.bottomView];
    
    //    [self requestShangPinLeiMiData]; //请求商品类目数据
    
    [YingYeShiJianKeFuDianHuaHelper obtainYingYeShiJianComplectionBlock:^(NSString * _Nonnull active, NSString * _Nonnull quiet) {
        [self.bottomView updateYingYeShiJianBegin:active end:quiet];
    }]; //请求业营业时间
    
    [self.customView requestShangPinLeiMiData]; //请求本页数据
    
    
    //2019-2-25 不要了自动展开了
//    [self newDemand2019215NoDataNotExpandCartList]; //2019-2-16购物车数据就自动展开,没有不展开
}


#pragma mark - 四 底部购物车事件 ShopCartViewDelegate
- (void)shopcartViewDidClickCartBtn:(UIButton *)btn {
    RBFunctionLog;
    [self newDemand2019215NoDataNotExpandCartList];
}


//!!!!:2019-2-16新加需求
/**
 1.一进来:如果有数据购物车自动展开
 2.如果没有数据点击“购物车按钮” 不展开购车,同时提示@"购物车暂无商品" ✅
 /user/cart/list
 */
- (void)newDemand2019215NoDataNotExpandCartList {
    [HomeShopCartHelper shopcartHelperCarListIsEmptyBlock:^(BOOL isHaveData) {
        FDLog(@"111:%@",isHaveData ? @"有数据" :@"没有数据不展开");
        
        if (isHaveData) { //有数据才暂开购物车列表
            [self expandCartListView];
        }else { //没有数据不暂开
            [JRToast showWithText:NSLocalizedString(Home_ShopCartNoGoods, @"购物车暂无商品")];
        }
    }];
}

- (void)expandCartListView {
    if (!_isShopping) {
        self.shopcartView=[[ShoppingCartView alloc]init];
        self.shopcartView.frame =CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH,SCREEN_HEIGHT-FD_NavH-FD_TabbarSafeBottomMargin);
        self.shopcartView.delegate = self; //设置代理
        
        //蒙版：蒙版为什么不放在里面呢？
        UIView *bgView =[[UIView alloc] initWithFrame:CGRectMake(0, FD_NavH, SCREEN_WIDTH,SCREEN_HEIGHT-50-FD_NavH-FD_TabbarSafeBottomMargin)];
        //        bgView.alpha = .7;
        bgView.tag = 111;
        //        bgView.backgroundColor =[UIColor lightGrayColor];
        bgView.backgroundColor = [UIColor rb_colorHex:0x000000 alpha:0.5];
        
        [self.view addSubview:bgView];
        
        //显示购物车
        [self.shopcartView addShoppingCartView:self];
        
        [UIView animateWithDuration:.3 animations:^{
            //适配iphoneX
            self.shopcartView.frame =CGRectMake(0, FD_NavH, SCREEN_WIDTH,SCREEN_HEIGHT-50-FD_NavH-FD_TabbarSafeBottomMargin);
        } completion:^(BOOL finished) {
        }];
    } else{
        [self.shopcartView removeSubView:self];
    }
    _isShopping = !_isShopping;
}




- (void)shopcartViewDidClickJieSuanBtnAction:(UIButton *)btn {
    RBFunctionLog;
    
    if (self.sumPrice < 0.1) {
        [JRToast showWithText:NSLocalizedString(Home_ShopCartNoGoods, @"购物车暂无商品")];
        return;
    }
    
    //2019-2-21移除购物车
    [self.shopcartView removeSubView:self];
    
    //跳转到确认订单页面
    YuYueSureOrderViewController  *sureOrderVC = [[YuYueSureOrderViewController alloc] init];
    sureOrderVC.yuyueId = self.yuyueId1;
    [self.navigationController pushViewController:sureOrderVC animated:YES];
}

#pragma mark - 四-2 ShoppingCartViewDelegate
/**用于更新底部总金额，添加移除购物车之后要，从新计算总价。并通过代理的方式通知控制器
 让控制器告知 ShopCartBottomView 更新总金额*/
- (void)shoppingCartViewUpdateTotalPrice:(CGFloat)totalPrice {
    RBFunctionLog
    [self.bottomView updateshopcartBottomViewTotalPrice:totalPrice];
}

/**购物车被清空通知控制器移除购物车页面*/
- (void)shoppingCartHaveBeClean {
    RBFunctionLog
    [self.shopcartView removeSubView:self]; //从当前控制器消失
    
    self.sumPrice = 0; //标记购物车金额是否为0
}

#pragma mark -    getter
//!!!!:点击跳转
- (ShangPinLeiMuCustomView *)customView {
    if (!_customView) {
        _customView = [[ShangPinLeiMuCustomView alloc] init];
        
        FDWeakSelf;
        //事件回调
        _customView.splmCustomViewClickCellblock = ^(SPLMModel * _Nonnull splmModel) {
            
            YuYueDianCanCartViewController *yuyueVC = [YuYueDianCanCartViewController new];
            yuyueVC.yuyueId = weakSelf.yuyueId1; //确认订单需要多此传递
            yuyueVC.shangPinLeiMuId = splmModel.splmId; //请求参数
            
            //跳转
            [weakSelf.navigationController pushViewController:yuyueVC animated:YES];
        };
    }
    return _customView;
}

- (NewShopCartBottomView *)bottomView {
    if (!_bottomView) {
        //view的高度
        CGFloat sh = self.view.bounds.size.height;
        
        CGFloat y = sh - 50-FD_NavH-FD_TabbarSafeBottomMargin;
        //设置的代理
        _bottomView = [[NewShopCartBottomView alloc] initWithFrame:CGRectMake(0, y, SCREEN_WIDTH, 50)];
        _bottomView.delegate = self; //设置代理 ShopCartViewDelegate
    }
    return _bottomView;
}
@end

